
import os
from pandas import DataFrame
from pandas import read_excel
import db


class Template:
    def __init__(self,TableName, *file):
        self.read = read_excel(file[0])
        self.__frame = DataFrame(self.read)
        self.data = []
        self.APPend = self.data.append
        self.sqlWord = []
        self.__sqlAppend = self.sqlWord.append
        self.TableName = TableName

    @staticmethod
    def TempLate(x):
        return list(map(lambda x : x.replace("\n", " "), x))

    def ImportWord(self):
        Link = db.DBOperate("")
        name = input("请输入单词本 名字:>")

        for i, j in self.__frame.iterrows():
            j = list(j)
            self.APPend(tuple(j[0 : 3]))
        data = (list(map(self.TempLate, self.data)))

        for word in data:
            self.__sqlAppend(tuple(word))
        if name in self.TableName:
            print("名字已存在，请重新输入！")
        else:
            Link.CreateTable(name)
        Link.InsertBook(table=name, data = self.sqlWord)

        return True
    def GenerateTemplate(self):
        column = ["单词", "词性", "词义"]
        frame = DataFrame([], columns=column,)
        frame.to_excel("Template.xlsx", index = False)
        os.startfile("Template.xlsx")
